﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.WarehouseManagement;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.WarehouseManagement
{
    /// <summary>
    /// 库存盘点仓储
    /// </summary>
    public class InventoryCountsServices : ApplicationService, IInventoryCountsServices
    {
        /// <summary>
        /// 库存盘点
        /// </summary>
        private readonly IRepository<InventoryCounts, int> _repositories;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repositories"></param>
        public InventoryCountsServices(IRepository<InventoryCounts, int> repositories)
        {
            _repositories = repositories;
        }
        /// <summary>
        /// 添加库存盘点
        /// </summary>
        /// <param name="inventoryCounts"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddInventoryCounts(InventoryCountsDTO inventoryCounts)
        {
            var entity = ObjectMapper.Map<InventoryCountsDTO, InventoryCounts>(inventoryCounts);
            var result = await _repositories.InsertAsync(entity);
            return result == null ? 0 : 1;
        }
        /// <summary>
        /// 删除库存盘点
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DelInventoryCounts(List<int> ids)
        {
            await _repositories.DeleteDirectAsync(m => ids.Contains(m.Id));
        }
        /// <summary>
        /// 获取库存盘点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<InventoryCountsDTO> GetInventoryCounts(int id)
        {
            var InventoryCountss = await _repositories.FindAsync(m => m.Id == id);
            var result = ObjectMapper.Map<InventoryCounts, InventoryCountsDTO>(InventoryCountss);
            return result;
        }
        /// <summary>
        /// 获取库存盘点列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="countingOrderName"></param>
        /// <returns></returns>
        public async Task<List<InventoryCountsDTO>> ShowInventoryCounts(int pageIndex = 1, int pageSize = 10, string? countingOrderName = "")
        {
            var list = await _repositories.GetListAsync();
            list = list.WhereIf(!string.IsNullOrEmpty(countingOrderName), m => m.CountingOrderName.Contains(countingOrderName)).ToList();
            var result = ObjectMapper.Map<List<InventoryCounts>, List<InventoryCountsDTO>>(list);
            return result;
        }
        /// <summary>
        /// 编辑库存盘点
        /// </summary>
        /// <param name="inventoryCounts"></param>
        /// <returns></returns>
        public async Task<int> UpdateInventoryCounts(InventoryCountsDTO inventoryCounts)
        {
            var entity = ObjectMapper.Map<InventoryCountsDTO, InventoryCounts>(inventoryCounts);
            var result = await _repositories.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }
    }
}
